package org.emergency.framework.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.emergency.framework.constant.SessionConstant;
import org.emergency.framework.model.TbPermission;
import org.emergency.framework.otherDatasourse.TbSysConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class Context {

	public static ServletContext servletContext;
	public static WebApplicationContext webApplicationContext;
	
	private static Map<String, TbSysConfig> tbSysConfiMap;
	/**
	 * 得到request对象
	 * @return  request
	 */
	public static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	/**
	 * 得到response对象
	 * @return  request
	 */
	public static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	/**
	 * 得到Session对象
	 * @return Session
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}
	
	/**
	 * 得到servletContent对象
	 * @return
	 */
	public static ServletContext getServletContext(){
		return servletContext; 
	}

	/**
	 * 得到spring上下文
	 * @return
	 */
	public static WebApplicationContext getWebApplicationContext() {
		//例子: HibernateDao dao = (HibernateDao)webApplicationContext.getBean("dao");
		return webApplicationContext;
	}
	
	/**
	 * 从spring配置文件中得到Bean的实例 
	 * @param beanName  名称
	 * @return  Bean 实例
	 */
	public static Object getSpringBean(String beanName){
		ServletContext servletContext = getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		return wac.getBean(beanName);
	}
	
	/**
	 * 判断当前用户是否对某个按钮具有操作权限
	 * @param operator
	 * @return
	 */
//	public static Boolean hasPrivilege(String actionName){
//		TbUser currentUser = getCurrentUser();
//		
////		AuthorityDao authDao =(AuthorityDao)getSpringBean("authorityDaoImpl");
//		ITbPermissionDAO tbPermissionDAO = (TbPermissionDAO)getSpringBean("tbPermissionDAO");
//		
//        List authList = tbPermissionDAO.findListByUser(currentUser);
//
//        for(int i=0;i<authList.size();i++){
//        	Object obj = (Object) authList.get(i);
//        	String authName = (String) obj;
//			if(actionName.contains(authName)){
//				 return true;
//			}
//        }
//		return false;
//	}
	
	/**
	 * 获取当前登录用户
	 * dgm..在session中，user从UserUtil.USER_SESSION的get方法中得到，
	 * USER_SESSION不是一个TbUser类
	 * @return
	 */
//	public static TbUser getCurrentUser(){
//		Object userSessionObject = getSession().getAttribute(UserConstant.USER_SESSION);
////		Object userObject = getSession().getAttribute(SessionConstant.SESSION_USER);
//		TbUser currentUser = null;
//		if(null!=userSessionObject){
//			currentUser = ((UserSession)userSessionObject).getTbUser();
//		}
//		return currentUser;
//	}
//	
//	
//	public static boolean isPasswordValid(String srcStr,String encodeStr){
//		return PasswordUtil.getInstance().encode(srcStr).equals(encodeStr)?true:false;
//	}
//	/**
//	 * 设置当前登录用户
//	 * @return
//	 */
//	public static void setCurrentUser(TbUser tbUser){
//		UserSession userSession = new UserSession();
//		userSession.setTbUser(tbUser);
//		if(null!=tbUser){
//			getSession().setAttribute(UserConstant.USER_SESSION, userSession);
//		}
//	}
	
	/**
	 * 获取上下文根路径
	 * @return
	 */
	public static String getContextPath(){
		return ServletActionContext.getRequest().getContextPath();
	}
	
	/**
	 * 获取上下文URL全路径
	 * @return
	 */
	public static String getContextAllPath(){
		return getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+getRequest().getContextPath();
	}
	
	public static String getParam(String keyString) {
		// TODO Auto-generated method stub
		Properties p = new Properties();
		InputStream in = null;
		try {
			//这里Context好像是可以任意类名,只要能引出class这个属性的getResourceAsStream方法 
			in = Context.class.getResourceAsStream("/CustomConfig.properties");
			p.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return p.getProperty(keyString);
	}
	
	
	public static List<String> getPermissonString(){
		UserSession usersession = (UserSession)Context.getSession().getAttribute(SessionConstant.UserSession);
		List<TbPermission> tbPermissions = usersession.getTbPermissionList();
		List<String> permissionStringList = new ArrayList<String>();
		for(TbPermission t : tbPermissions){
			permissionStringList.add(t.getPId());
		}
		return permissionStringList;
	}
	
	/**
	 * 配置的条目
	 * @param item
	 * @return
	 */
	public static TbSysConfig getTbSysConfigByitem(String item){
		return tbSysConfiMap.get(item);
	}
	
	public static void addtbSysConf(String s,TbSysConfig tbSysConfig){
		tbSysConfiMap.put(s, tbSysConfig);
	}
	
	
}

